﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace ExFlash.Records
{
    [TypeConverter(typeof(ShapeConverter))]
    public class Shape : ISwfSerializable
    {
        protected ShapeType ShapeType;

        [Browsable(false)]
        public Byte NumFillBits
        {
            get;
            protected set;
        }

        [Browsable(false)]
        public Byte NumLineBits
        {
            get;
            protected set;
        }

        public FillStyleArray FillStyles
        {
            get;
            protected set;
        }

        public LineStyleArray LineStyles
        {
            get;
            protected set;
        }

        public List<ShapeRecord> ShapeRecords
        {
            get;
            protected set;
        }

        public Shape(ShapeType type)
        {
            ShapeType = type;
        }

        #region ISwfSerializable Members

        public virtual void SwfDeserialize(BinaryBitReader reader)
        {
            NumFillBits = (Byte)reader.ReadUInt32(4);
            NumLineBits = (Byte)reader.ReadUInt32(4);
            reader.AlignBits();

            ShapeRecords = new List<ShapeRecord>();

            while ((reader.PeekByte(true) >> 2) > 0)
            {
                ShapeRecord rec;

                Boolean isEdge = reader.ReadBoolean();

                if (isEdge)
                {
                    if (reader.PeekBoolean())
                    {
                        rec = new StrightEdgeRecord();
                    }
                    else
                    {
                        rec = new CurvedEdgeRecord();
                    }
                }
                else
                {
                    rec = new StyleChangeRecord(NumFillBits, NumLineBits, ShapeType);
                }

                rec.SwfDeserialize(reader);

                StyleChangeRecord changeRec = rec as StyleChangeRecord;

                if (changeRec != null)
                {
                    if (changeRec.StateNewStyles)
                    {
                        NumFillBits = changeRec.NumFillBits;
                        NumLineBits = changeRec.NumLineBits;
                        //FillStyles = ChangeRec.FillStyles;
                        //LineStyles = ChangeRec.LineStyles;
                    }
                }

                ShapeRecords.Add(rec);
            }

            // Usually the bit is read inside the loop.
            reader.SkipBits(1);

            EndShapeRecord endRec = new EndShapeRecord();
            endRec.SwfDeserialize(reader);
            ShapeRecords.Add(endRec);
        }

        public virtual void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class ShapeConverter : ExpandableObjectConverter
    {

    }
}
